package redsoxfan.gamejam.base;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

/**
 * Wrapper for a desktop game. The wrapper supports full screen toggling. An applet is used for this wrapper.
 * Usage: Use redsoxfan.gamejam.base.AppletWrapper.class for code and set a param with the key "gameclass" to
 * the fully qualified name of the class for the game.
 * @author RedSoxFan
 * @since 20 Apr 14
 * @version 1.0
 */
public class AppletWrapper extends Applet implements GameWrapper {
	/**
	 * The default screen
	 */
	private static final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
	/**
	 * The full screen window
	 */
	private JFrame frame;
	/**
	 * The game instance
	 */
	private Game game;
	/**
	 * Initialization of the applet. Sets up applet settings and full screen support
	 */
	public void init() {
		try {
			Object g = Class.forName(getParameter("gameclass")).newInstance();
			if (g instanceof Game)
				game = (Game) g;
		} catch (Exception e) {
			// Suppress Error
		}
		if (game == null) {
			game = new Game() {
				/**
				 * Authors of the game
				 */
				public String authors() {
					return "RedSoxFan";
				}
				/**
				 * Painting code
				 */
				public void draw(Graphics g) {
				}
				/**
				 * Icon to use for the window
				 */
				public Image icon() {
					return new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
				}
				/**
				 * Initialization code
				 */
				public void init() {
				}
				/**
				 * A short title for the game. This will be used for the distribution files. Make sure it does
				 * not include any illegal file characters!
				 * @return Short title
				 */
				public String shortTitle() {
					return "GameBase";
				}
				/**
				 * Code to execute each frame
				 */
				public void tick() {
				}
				/**
				 * Title of the game
				 */
				public String title() {
					return "Game Base";
				}
			};
		}
		game.start(this);
		setSize(game.width(), game.height());
		setLayout(new BorderLayout());
		add(game, BorderLayout.CENTER);
		frame = new JFrame(game.title());
		frame.setIconImage(game.icon());
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setUndecorated(true);
        game.addKeyListener(new KeyAdapter(){
        	public void keyReleased(KeyEvent event) {
        		if (event.getKeyCode() == game.fullscreenKey()) {
        			setFullscreen(!isFullscreen());
        		}
        	}
        });
	}
	/**
	 * Returns whether the wrapper is an applet. For this wrapper, it is true.
	 * @return true
	 */
	public boolean isApplet() {
		return true;
	}
	/**
	 * Returns whether the wrapper is running on the desktop. For this wrapper, it is false.
	 * @return false
	 */
	public boolean isDesktop() {
		return false;
	}
	/**
	 * Returns whether the game is being displayed in full screen
	 * @return Full screen state
	 */
	public boolean isFullscreen() {
		return frame.isVisible();
	}
	/**
	 * Set the full screen state
	 * @param fulls - Desired full screen state
	 */
	public void setFullscreen(boolean fulls) {
        if (fulls) {
		   	frame.add(game, BorderLayout.CENTER);
		    game.setPreferredSize(new Dimension(1,1));
		    frame.setVisible(true);
		    game.requestFocus();
		    device.setFullScreenWindow(frame); // Re-Associate popout with full screen.
        } else {
            add(game, BorderLayout.CENTER);
            game.setSize(new Dimension(game.width(), game.height()));
            frame.setVisible(false);
            game.requestFocus();
            device.setFullScreenWindow(null); // Un-Associate popout with full screen.
        }
    }
}
